package com.htjc.integral.account.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.htjc.integral.account.service.IAccountService;
import com.htjc.integral.account.util.JsonUtil;
import com.htjc.integral.account.util.ResultModel;
import com.htjc.integral.account.util.enums.AccountReturn;
import com.htjc.integral.common.annotation.InvokeLog;
import com.htjc.integral.common.util.StringUtil;

/**
 * 账户中心  controller层
 * 请求参数请做好日志记录
 *
 * @author zhangjiajia
 */
@RestController
@RequestMapping(value = "/ac/account")
public class AccountController {
	
	@Autowired
	private IAccountService accountService;

	/**
	 * 交费用户积分划转
	 * @param json
	 * @return json
	 */
	@InvokeLog(value="交费用户积分划转",params={"ACCOUNT_MAIN_ID","LOGIN_ID","UN_ID","LIST","POWERUSERNOLIST"})
	@RequestMapping(value = {"/payChangeIntegral"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String payChangeIntegral(@RequestBody String params){
		//1、校验参数
		JSONObject json = JSON.parseObject(params);
		json.put("IP","127.0.0.1");
//		headerMap.put("CHANNEL_RESOURCE",json.getString("CHANNEL_NO"));//渠道编码
		json.put("CHANNEL_RESOURCE","5");//渠道编码
		json.put("USER_NAME","网上国网");//
		//循环域
		JSONArray listJson = JSON.parseArray(json.getString("LIST"));
		for(int i=0;i<listJson.size();i++) {
			// 遍历array数组,把每一个对象转成 json 对象
			JSONObject jsonObject = listJson.getJSONObject(i);
			//校验网省编号
			if (StringUtil.isNull(jsonObject.getString("PROVINCE_CODE"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.PROVINCE_CODE_ERROR_NULL))).toString();
			}
			//校验供电单位编码
			if (StringUtil.isNull(jsonObject.getString("ORG_NO"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.ORG_NO_ERROR_NULL))).toString();
			}
			//校验户号ID
			if (StringUtil.isNull(jsonObject.getString("POWER_USER_NO"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.POWER_USER_NO_ERROR_NULL))).toString();
			}
			//校验交费金额
			if (StringUtil.isNull(jsonObject.getString("PAY_NUM"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.PAY_NUM_ERROR_NULL))).toString();
			}
			//校验交费时间
			if (StringUtil.isNull(jsonObject.getString("CREATE_TIME"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.CREATE_TIME_ERROR_NULL))).toString();
			}
		}
		return accountService.payChangeIntegralByDxp(json).toString();
	}
	/**
	 * 交费用户积分划转（new）
	 * @param json
	 * @return json
	 */
	@InvokeLog(value="交费用户积分划转（new）",params={"ACCOUNT_MAIN_ID","LOGIN_ID","UN_ID","LIST","POWERUSERNOLIST"})
	@RequestMapping(value = {"/payChangeIntegralByPower"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String payChangeIntegralByPower(@RequestBody String params){
		//1、校验参数
		JSONObject json = JSON.parseObject(params);
		//循环域
		JSONArray listJson = JSON.parseArray(json.getString("LIST"));
		for(int i=0;i<listJson.size();i++) {
			// 遍历array数组,把每一个对象转成 json 对象
			JSONObject jsonObject = listJson.getJSONObject(i);
			//校验网省编号
			if (StringUtil.isNull(jsonObject.getString("PROVINCE_CODE"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.PROVINCE_CODE_ERROR_NULL))).toString();
			}
			//校验供电单位编码
			if (StringUtil.isNull(jsonObject.getString("ORG_NO"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.ORG_NO_ERROR_NULL))).toString();
			}
			//校验户号ID
			if (StringUtil.isNull(jsonObject.getString("POWER_USER_NO"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.POWER_USER_NO_ERROR_NULL))).toString();
			}
			//校验交费金额
			if (StringUtil.isNull(jsonObject.getString("PAY_NUM"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.PAY_NUM_ERROR_NULL))).toString();
			}
			//校验交费时间
			if (StringUtil.isNull(jsonObject.getString("CREATE_TIME"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.CREATE_TIME_ERROR_NULL))).toString();
			}
		}
		return accountService.payChangeIntegralByPower(json).toString();
	}

	/**
	 * 用户点击积分中心积分划转（new）
	 * @return json
	 */
//	@InvokeLog(value="用户点击积分中心积分划转",params={"PROVINCE_CODE","ORG_NO","BUSINESS_TYPE","BUSINESS_ID","LOGIN_ACCOUNT_ID"})
	@InvokeLog(value="用户点击积分中心积分划转（new）",params={"LOGIN_ACCOUNT_ID","POWERUSERNOLIST"})
	@RequestMapping(value = {"/changeIntegralByPower"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String changeIntegralByPower(@RequestBody String params){
		JSONObject json = JSON.parseObject(params);
		return accountService.changeIntegralByPower(json).toString();
	}
	/**
	 * 用户点击积分中心积分划转
	 * @param json( PROVINCE_CODE	网省编号
	 *				ORG_NO	供电单位编码
	 *				BUSINESS_TYPE	业务类型
	 *				BUSINESS_ID	业务ID
	 *				LOGIN_ACCOUNT_ID	登录账户ID)
	 * @return json
	 */
//	@InvokeLog(value="用户点击积分中心积分划转",params={"PROVINCE_CODE","ORG_NO","BUSINESS_TYPE","BUSINESS_ID","LOGIN_ACCOUNT_ID"})
	@InvokeLog(value="用户点击积分中心积分划转",params={"LOGIN_ACCOUNT_ID"})
	@RequestMapping(value = {"/changeIntegral"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String changeIntegral(@RequestBody String params){
		JSONObject json = JSON.parseObject(params);
		json.put("IP","127.0.0.1");
//		headerMap.put("CHANNEL_RESOURCE",json.getString("CHANNEL_NO"));//渠道编码
		json.put("CHANNEL_RESOURCE","5");//渠道编码
		json.put("USER_NAME","网上国网");//
		return accountService.changeIntegralByDxp(json).toString();
	}



	/**
	 * 用户绑定关系同步
	 * @param params(BUSINESS_ID	业务ID
	 *			   LOGIN_ACCOUNT_ID	登录账户ID
	 *				POWER_USER_NO	户号ID
	 *				IS_BOUND	绑定关系
	 *				BOUND_TIME	绑定时间)
	 * @return
	 */
	@InvokeLog(value="用户绑定关系同步",params={"LOGIN_ACCOUNT_ID","BUSINESS_ID","POWER_USER_NO","IS_BOUND","BOUND_TIME","PROVINCE_ID","ORG_NO"})
	@RequestMapping(value = {"/bindingSync"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String bindingSync(@RequestBody String params){
		JSONObject json = JSON.parseObject(params);
		return accountService.bindingSync(json).toString();
	}



	/**
	 * 账户中心查询接口
	 */
	@InvokeLog(value="积分余额查询",params={"LOGIN_ACCOUNT_ID"})
	@RequestMapping(value = {"/queryIntegral"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String queryIntegral(@RequestBody String params) {
		JSONObject json = JSON.parseObject(params);
		json.put("IP","127.0.0.1");
//		headerMap.put("CHANNEL_RESOURCE",json.getString("CHANNEL_NO"));//渠道编码
		json.put("CHANNEL_RESOURCE","5");//渠道编码
		json.put("USER_NAME","网上国网");//
		return accountService.queryIntegral(json).toString();
	}

	/**
	 *  账户中心扣减接口
	 *
	 * @param jsonObject ACCOUNT_NAME 账户名称
	 *                   LOGIN_ACCOUNT_ID 登录账户ID
	 *                   OPERATION_STATUS 操作类型
	 *                   EXPENDITURE_WHERE 业务类型
	 *                   EXPENDITURE_DESCRIBE 支出备注()
	 *                   EXPENDITURE_NUM 支出金额
	 *                   WHEREABOUTS_ID 去向方账号
	 *                   BUSINESS_ID 业务id
	 * @return
	 * @throws Exception
	 */
	@InvokeLog(value="积分余额扣减",params={"LOGIN_ACCOUNT_ID","OPERATION_STATUS","EXPENDITURE_DESCRIBE","EXPENDITURE_NUM","BUSINESS_ID"})
	@RequestMapping(value = {"/deductionIntegral"}, produces = {"application/json;"})
	public String deductionIntegral( @RequestBody String jsonObject) throws Exception {
		JSONObject json=JSONObject.parseObject(jsonObject);
		json.put("IP","127.0.0.1");
//		headerMap.put("CHANNEL_RESOURCE",json.getString("CHANNEL_NO"));//渠道编码
		json.put("CHANNEL_RESOURCE","5");//渠道编码
		json.put("USER_NAME","网上国网");//
		return accountService.deductionIntegral(json).toString();
	}

	/**
	 * 账户中心添加接口
	 *
	 * @param jsonObject LOGIN_ACCOUNT_ID	登录账户ID
	 *BUSINESS_ID	业务ID
	 *INCOME_SOURCE	收入来源
	 *OPERATION_STATUS	操作类型
	 *SOURCE_DESCRIBE	来源说明（）
	 *INCOME_NUM	收入总金额
	 * @return
	 * @throws Exception
	 */
	@InvokeLog(value="积分余额增加",params={"LOGIN_ACCOUNT_ID","BUSINESS_ID","INCOME_SOURCE","OPERATION_STATUS","SOURCE_DESCRIBE","INCOME_NUM"})
	@RequestMapping(value = {"/addIntegral"}, produces = {"application/json;"})
	public String addIntegral(@RequestBody String jsonObject) throws Exception {
		JSONObject json = JSON.parseObject(jsonObject);
		json.put("IP","127.0.0.1");
//		headerMap.put("CHANNEL_RESOURCE",json.getString("CHANNEL_NO"));//渠道编码
		json.put("CHANNEL_RESOURCE","5");//渠道编码
		json.put("USER_NAME","网上国网");//
		return accountService.addIntegral(json).toString();
	}

	/**
	 * 账户中心查询积分明细接口
	 * @param params
	 * UNION_ID	统一用户ID
	 * CHANNEL_NO	渠道编码
	 * IP	当前请求IP
	 * PAGE_INDEX	页码
	 * @return
	 */
//	@InvokeLog(value="积分明细查询",params={"UNION_ID","CHANNEL_RESOURCE","IP","PAGE_INDEX"})
	@InvokeLog(value="积分明细查询",params={"UNION_ID","IP","PAGE_INDEX"})
	@RequestMapping(value = {"/queryIntegralDetail"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String queryIntegralDetail(@RequestBody String params) {
		JSONObject json = JSON.parseObject(params);
		json.put("IP","127.0.0.1");
//		headerMap.put("CHANNEL_RESOURCE",json.getString("CHANNEL_NO"));//渠道编码
		json.put("CHANNEL_RESOURCE","5");//渠道编码
		json.put("USER_NAME","网上国网");//
		return accountService.queryIntegralDetail(json).toString();
	}
	
	
	
}
